.TH std::array::end,std::array::cend 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::array::end,std::array::cend \- std::array::end,std::array::cend

.SH Synopsis
   iterator end() noexcept;              \fB(1)\fP \fI(since C++11)\fP
                                             (constexpr since C++17)
   const_iterator end() const noexcept;  \fB(2)\fP \fI(since C++11)\fP
                                             (constexpr since C++17)
   const_iterator cend() const noexcept; \fB(3)\fP \fI(since C++11)\fP
                                             (constexpr since C++17)

   Returns an iterator to the element following the last element of the array.

   This element acts as a placeholder; attempting to access it results in undefined
   behavior.

   range-begin-end.svg

.SH Parameters

   \fI(none)\fP

.SH Return value

   Iterator to the element following the last element.

.SH Complexity

   Constant.

.SH Example


// Run this code

 #include <algorithm>
 #include <array>
 #include <iomanip>
 #include <iostream>

 int main()
 {
     std::cout << std::boolalpha;

     std::array<int, 0> empty;
     std::cout << "1) "
               << (empty.begin() == empty.end()) << ' '     // true
               << (empty.cbegin() == empty.cend()) << '\\n'; // true
     // *(empty.begin()) = 42; // => undefined behavior at run-time


     std::array<int, 4> numbers{5, 2, 3, 4};
     std::cout << "2) "
               << (numbers.begin() == numbers.end()) << ' '    // false
               << (numbers.cbegin() == numbers.cend()) << '\\n' // false
               << "3) "
               << *(numbers.begin()) << ' '    // 5
               << *(numbers.cbegin()) << '\\n'; // 5

     *numbers.begin() = 1;
     std::cout << "4) " << *(numbers.begin()) << '\\n'; // 1
     // *(numbers.cbegin()) = 42; // compile-time error:
                                  // read-only variable is not assignable

     // print out all elements
     std::cout << "5) ";
     std::for_each(numbers.cbegin(), numbers.cend(), [](int x)
     {
         std::cout << x << ' ';
     });
     std::cout << '\\n';

     constexpr std::array constants{'A', 'B', 'C'};
     static_assert(constants.begin() != constants.end());   // OK
     static_assert(constants.cbegin() != constants.cend()); // OK
     static_assert(*constants.begin() == 'A');              // OK
     static_assert(*constants.cbegin() == 'A');             // OK
     // *constants.begin() = 'Z'; // compile-time error:
                                  // read-only variable is not assignable
 }

.SH Output:

 1) true true
 2) false false
 3) 5 5
 4) 1
 5) 1 2 3 4

.SH See also

   begin   returns an iterator to the beginning
   cbegin  \fI(public member function)\fP
   end
   cend    returns an iterator to the end of a container or array
   \fI(C++11)\fP \fI(function template)\fP
   \fI(C++14)\fP
